package com.example.demo;

import com.example.demo.manager.CustomGroupEntityManagerFactory;
import com.example.demo.manager.CustomUserEntityManagerFactory;
import org.activiti.engine.impl.interceptor.SessionFactory;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.activiti.spring.boot.SecurityAutoConfiguration;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import java.util.ArrayList;
import java.util.List;

@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
@MapperScan("com.example.demo.dao")
public class ActivitiApplication {

	@Autowired
	private SpringProcessEngineConfiguration springProcessEngineConfiguration;

	@Autowired
	private CustomGroupEntityManagerFactory customGroupEntityManagerFactory;

	@Autowired
	private CustomUserEntityManagerFactory customUserEntityManagerFactory;

	@Bean
	public CommandLineRunner init(){
		return new CommandLineRunner() {

			@Override
			public void run(String... args) throws Exception {
				List<SessionFactory> customSessionFactories = new ArrayList<SessionFactory>();
				customSessionFactories.add(customUserEntityManagerFactory);
				customSessionFactories.add(customGroupEntityManagerFactory);
				springProcessEngineConfiguration.setCustomSessionFactories(customSessionFactories);
				//此处必须要重新初始化引擎
				springProcessEngineConfiguration.buildProcessEngine();
			}
		};
	}

	public static void main(String[] args) {

		SpringApplication.run(ActivitiApplication.class, args);
	}
}
